﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Hi_Baidu.FileShare;
using Hi_Baidu.FileShare.ListType;
using Hi_Baidu.UI.Compoment;

namespace Hi_Baidu.FileShare.Model
{
    public class TransferFileModel
    {
        public static bool TransferFileToUser(SendFileControl sendFileControl, String FriendIpAddress, byte[] MySelfFilePort, int FriendFilePort, byte[] FileSession)
        {
            //如果文件存在
            if (File.Exists(sendFileControl.FilePath))
            {
                #region -- 检测传送文件记录 --
                //检测传送文件记录
                TYPE_RecvFileTotalInfomation RecvFileTotalInfomation = CreateTransferFileToManager(sendFileControl, FileSession);
                byte[] FileContent = null;
                #endregion

                #region -- 读取文件 --
                //读取文件
                using(FileStream FS_FileStream = new FileStream(sendFileControl.FilePath,FileMode.Open))
                {
                    FileContent = new byte[FS_FileStream.Length];
                    FS_FileStream.Position = 0;
                    FS_FileStream.Read(FileContent, 0, FileContent.Length);
                }
                #endregion

                //将文件加载入内存流
                using (MemoryStream MS_FileStream = new MemoryStream())
                {
                    MS_FileStream.Write(FileContent, 0, FileContent.Length);
                    MS_FileStream.Position = 0;
                }
                
                return true;
            }
            //文件不存在
            else
            {
                return false;
            }
        }

        /// <summary>
        ///     检测传送文件记录
        /// </summary>
        /// <param name="sendFileControl" type="Hi_Baidu.UI.Compoment.SendFileControl">
        ///     <para>
        ///         文件相关信息控件
        ///     </para>
        /// </param>
        /// <param name="FileSession" type="byte[]">
        ///     <para>
        ///         当前要传送文件的会话ID
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回当前存在于传送文件集合里面的文件传送记录
        /// </returns>
        private static TYPE_RecvFileTotalInfomation CreateTransferFileToManager(SendFileControl sendFileControl, byte[] FileSession)
        {
            if (!Globle.sendFileManager.Equals(Encoding.Default.GetString(FileSession)))
            {
                TYPE_RecvFileTotalInfomation RecvFileTotalInfomation = new TYPE_RecvFileTotalInfomation(Encoding.Default.GetString(FileSession));
                RecvFileTotalInfomation.FileName = sendFileControl.FileName;
                RecvFileTotalInfomation.FilePath = sendFileControl.Key;
                RecvFileTotalInfomation.FileExt = sendFileControl.FileExt;
                RecvFileTotalInfomation.FileSession = sendFileControl.SENDF_FILE_SESSION_CODE;
                RecvFileTotalInfomation.FileSize = sendFileControl.FileSize;
                Globle.sendFileManager.Add(RecvFileTotalInfomation);
                return RecvFileTotalInfomation;
            }
            else
            {
                return Globle.sendFileManager.GetMemberByKey(Encoding.Default.GetString(FileSession)) as TYPE_RecvFileTotalInfomation;
            }
        }
    }
}
